package app.controllers;

import app.models.SpatialModel;
import framework.FRDModel;
import framework.spatial.FRDSpatial;
import framework.spatial.FRDSpatialController;
import framework.spatial.FRDSpatialGItem;
import java.awt.event.ActionEvent;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

/**
 *
 * @author Lukáš Marek
 */
public class SpatialController extends FRDSpatialController
{
	private JComboBox type;
	public SpatialController(FRDSpatial panel, FRDModel model)
	{
		super(panel, model);
		AddComponents();
	}
	
	/**
	 * Vlozeni novych grafickych komponent
	 */
	private void AddComponents()
	{
		JLabel jLabel1 = new JLabel("Typ:");
		jLabel1.setBounds(20, 100, 40, 20);
		spatialPanel.jPanel1.add(jLabel1);
		
		type = new JComboBox();
		type.setBounds(90, 100, 230, 20);
		spatialPanel.jPanel1.add(type);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		switch (button.getActionCommand())
		{
			case "saveButton":
				FRDSpatialGItem item = spatialPanel.getSpatialMap().selectedItem;
				if (item != null)
				{
					LinkedHashMap<Integer,String> list = ((SpatialModel)model).getTypes();
					LinkedList<String> values = new LinkedList<>(list.values());
					LinkedList<Integer> ids = new LinkedList<>(list.keySet());
					
					LinkedList<String> params = new LinkedList<>();
					params.add(spatialPanel.nameTextField.getText());
					int id = ids.get(values.indexOf(type.getSelectedItem()));
					if (id != -1)
						params.add(String.valueOf(id));
					else
						params.add("0");
					params.add(spatialPanel.fromTextField.getText());
					params.add(spatialPanel.toTextField.getText());
					((SpatialModel)model).edit(item.getId(), params);
				}
				break;
			default:
				super.actionPerformed(e);
				break;
		}
	}
	
	@Override
	public void selectedItem(FRDSpatialGItem selectedItem)
	{
		if (selectedItem == null)
		{
			type.setModel(new DefaultComboBoxModel());
			spatialPanel.nameTextField.setText("");
			spatialPanel.fromTextField.setText("");
			spatialPanel.toTextField.setText("");
		}
		else
		{
			LinkedList<String> item = ((SpatialModel)model).select(selectedItem.getId());
			LinkedHashMap<Integer,String> list = ((SpatialModel)model).getTypes();
			LinkedList<String> values = new LinkedList<>(list.values());
			LinkedList<Integer> ids = new LinkedList<>(list.keySet());
			
			type.setModel(new DefaultComboBoxModel(values.toArray(new String[0])));
			if (item.get(1) == null)
			{
				type.setSelectedIndex(0);
			}
			else
			{
				int idIndex = ids.indexOf(Integer.parseInt(item.get(1)));
				if (idIndex != -1)
				{
					type.setSelectedIndex(idIndex);
				}
			}
			
			spatialPanel.nameTextField.setText(item.get(0));
			spatialPanel.fromTextField.setText(item.get(2));
			spatialPanel.toTextField.setText(item.get(3));
		}
		super.selectedItem(selectedItem);
	}
}
